package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.DeptmentService;
import com.xingkeduo.business.operation.service.ManagerService;
import com.xingkeduo.business.operation.service.PostsService;
import com.xingkeduo.pojo.dto.Dept.DeptmentDTO;
import com.xingkeduo.pojo.query.DeptmentQuery;
import com.xingkeduo.pojo.query.ManagerQuery;
import com.xingkeduo.pojo.query.PostsQuery;
import com.xingkeduo.pojo.table.DeptmentTable;
import com.xingkeduo.pojo.table.ManagerTable;
import com.xingkeduo.pojo.table.PostsTable;
import com.xingkeduo.tool.DateTimeUtils;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class DeptmentDomain {

    @Autowired
    private DeptmentService deptmentService;

    @Autowired
    private PostsService postsService;

    @Autowired
    private ManagerService managerService;

    //查看
    public DeptmentTable getDeptmentById(Long id) throws Throwable {
        DeptmentTable deptmentTable = deptmentService.selectById(id);
        return deptmentTable;
    }

    //获取列表
    public List<DeptmentTable> getDeptmentList(DeptmentQuery deptmentQuery) throws Throwable {
        List<DeptmentTable> deptmentList = deptmentService.selectLists(deptmentQuery);

        return deptmentList;
    }

    //分页查询
    public Page<DeptmentDTO> getDeptmentPageList(DeptmentQuery deptmentQuery) throws Throwable {
        Integer pageNo = deptmentQuery.getPageNo();
        Integer pageSize = deptmentQuery.getPageSize();
        Page<DeptmentTable> deptmentTablePage = deptmentService.selectPageLists(deptmentQuery,pageNo,pageSize);

        List<DeptmentTable> dtoList = deptmentTablePage.getDtoList();
        ArrayList<DeptmentDTO> objects = new ArrayList<>();
        if(!CommonUtil.isEmpty(dtoList)){
            for (DeptmentTable deptmentTable : dtoList) {
                DeptmentDTO vendorDTO = new DeptmentDTO();
                BeanUtils.copyProperties(deptmentTable,vendorDTO);
                int postNum = postsService.count(new PostsQuery().setDeptId(deptmentTable.getId()).setStatus(1));
                vendorDTO.setPostNum(postNum);
                int manager = managerService.count(new ManagerQuery().setDeptmentId(deptmentTable.getId()).setStatus(1));
                vendorDTO.setManagerNum(manager);
                vendorDTO.setCreateTimes(DateTimeUtils.yyyy_MM_dd_HH_mmToDate(deptmentTable.getCreatedTime()));
                objects.add(vendorDTO);
            }
        }

        Page<DeptmentDTO> vendorDTOPage = new Page<>(pageNo,pageSize);
        BeanUtils.copyProperties(deptmentTablePage, vendorDTOPage);
        vendorDTOPage.setDtoList(objects);
        return vendorDTOPage;
    }

    public int createDeptment(DeptmentTable deptmentTable) throws Throwable {

       if(!CommonUtil.isEmpty(deptmentTable.getName())){
           List<DeptmentTable> deptmentTableList = deptmentService.selectLists(new DeptmentQuery().setNameRepeat(deptmentTable.getName()));
           if(!CommonUtil.isEmpty(deptmentTableList)){
               throw new DataException(DataException.Type.DEFAULT, "岗位名字已经存在");
           }
       }
        return deptmentService.insert(deptmentTable);
    }

    public int modifyDeptment(DeptmentTable deptmentTable) throws Throwable {

        DeptmentTable deptmentTable1 = deptmentService.selectById(deptmentTable.getId());
       if(deptmentTable1 != null && !CommonUtil.isEmpty(deptmentTable.getName())){
           if(!deptmentTable.getName().equals(deptmentTable1.getName())){
               List<DeptmentTable> deptmentTableList = deptmentService.selectLists(new DeptmentQuery().setNameRepeat(deptmentTable.getName()));
               if(!CommonUtil.isEmpty(deptmentTableList)){
                   throw new DataException(DataException.Type.DEFAULT, "部门名字已经存在");
               }
           }
       }
        if(!CommonUtil.isEmpty(deptmentTable.getStatus())){
            if(deptmentTable.getStatus() == 2){
                List<PostsTable> postsTableList= postsService.selectLists(new PostsQuery().setDeptId(deptmentTable.getId()).setStatus(1));
                if(!CommonUtil.isEmpty(postsTableList) && postsTableList.size() > 0){
                    throw new DataException(DataException.Type.DEFAULT, "该部门下，还存在“启用”状态的岗位，无法停用部门!");
                }
            }
        }
        int update = deptmentService.update(deptmentTable);
        if(!CommonUtil.isEmpty(deptmentTable.getName())){
            if(update > 0){
                List<ManagerTable> managerTableList = managerService.selectLists(new ManagerQuery().setDeptmentId(deptmentTable.getId()));
                for (ManagerTable managerTable : managerTableList) {
                    ManagerTable updateManagerDeptName = new ManagerTable();
                    updateManagerDeptName.setId(managerTable.getId())
                            .setDeptmentName(deptmentTable.getName());
                    managerService.update(updateManagerDeptName);
                }
            }
        }
        return update;
    }

    //删除
    public int delDeptment(Long id) throws Throwable {

        //判断部门下是否有启用的岗位
        if(!CommonUtil.isEmpty(id)){
            List<PostsTable> postsTableList= postsService.selectLists(new PostsQuery().setDeptId(id).setStatus(1));
            if(!CommonUtil.isEmpty(postsTableList) && postsTableList.size() > 0){
                throw new DataException(DataException.Type.DEFAULT, "该分公司下，还存在“启用”状态的门店，无法删除该分公司!");
            }
        }


        DeptmentTable deptmentTable = new DeptmentTable();

        deptmentTable.setId(id);
        deptmentTable.setIsDelete(YesNoEnum.YES);
        int delRes = deptmentService.delete(deptmentTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

}
